home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing;
-
- import com.sun.java.swing.border.Border;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Insets;
- import java.awt.LayoutManager;
- import java.awt.Rectangle;
- import java.io.Serializable;
-
- public class ScrollPaneLayout implements LayoutManager, ScrollPaneConstants, Serializable {
- protected JViewport viewport;
- protected JScrollBar vsb;
- protected JScrollBar hsb;
- protected JViewport rowHead;
- protected JViewport colHead;
- protected Component lowerLeft;
- protected Component lowerRight;
- protected Component upperLeft;
- protected Component upperRight;
- protected int vsbPolicy = 20;
- protected int hsbPolicy = 30;
-
- public void addLayoutComponent(String s, Component c) {
- if (s.equals("VIEWPORT")) {
- this.viewport = (JViewport)this.addSingletonComponent(this.viewport, c);
- } else if (s.equals("VERTICAL_SCROLLBAR")) {
- this.vsb = (JScrollBar)this.addSingletonComponent(this.vsb, c);
- } else if (s.equals("HORIZONTAL_SCROLLBAR")) {
- this.hsb = (JScrollBar)this.addSingletonComponent(this.hsb, c);
- } else if (s.equals("ROW_HEADER")) {
- this.rowHead = (JViewport)this.addSingletonComponent(this.rowHead, c);
- } else if (s.equals("COLUMN_HEADER")) {
- this.colHead = (JViewport)this.addSingletonComponent(this.colHead, c);
- } else if (s.equals("LOWER_LEFT_CORNER")) {
- this.lowerLeft = this.addSingletonComponent(this.lowerLeft, c);
- } else if (s.equals("LOWER_RIGHT_CORNER")) {
- this.lowerRight = this.addSingletonComponent(this.lowerRight, c);
- } else if (s.equals("UPPER_LEFT_CORNER")) {
- this.upperLeft = this.addSingletonComponent(this.upperLeft, c);
- } else {
- if (!s.equals("UPPER_RIGHT_CORNER")) {
- throw new IllegalArgumentException("invalid layout key " + s);
- }
-
- this.upperRight = this.addSingletonComponent(this.upperRight, c);
- }
-
- }
-
- protected Component addSingletonComponent(Component oldC, Component newC) {
- if (oldC != null && oldC != newC) {
- oldC.getParent().remove(oldC);
- }
-
- return newC;
- }
-
- public JViewport getColumnHeader() {
- return this.colHead;
- }
-
- public Component getCorner(String key) {
- if (key.equals("LOWER_LEFT_CORNER")) {
- return this.lowerLeft;
- } else if (key.equals("LOWER_RIGHT_CORNER")) {
- return this.lowerRight;
- } else if (key.equals("UPPER_LEFT_CORNER")) {
- return this.upperLeft;
- } else {
- return key.equals("UPPER_RIGHT_CORNER") ? this.upperRight : null;
- }
- }
-
- public JScrollBar getHorizontalScrollBar() {
- return this.hsb;
- }
-
- public int getHorizontalScrollBarPolicy() {
- return this.hsbPolicy;
- }
-
- public JViewport getRowHeader() {
- return this.rowHead;
- }
-
- public JScrollBar getVerticalScrollBar() {
- return this.vsb;
- }
-
- public int getVerticalScrollBarPolicy() {
- return this.vsbPolicy;
- }
-
- public JViewport getViewport() {
- return this.viewport;
- }
-
- public Rectangle getViewportBorderBounds(JScrollPane sp) {
- Rectangle borderR = new Rectangle(((Component)sp).getSize());
- Insets insets = ((JComponent)sp).getInsets();
- borderR.x = insets.left;
- borderR.y = insets.top;
- borderR.width -= insets.left + insets.right;
- borderR.height -= insets.top + insets.bottom;
- if (this.colHead != null && this.colHead.isVisible()) {
- int colHeadHeight = this.colHead.getHeight();
- borderR.y += colHeadHeight;
- borderR.height -= colHeadHeight;
- }
-
- if (this.rowHead != null && this.rowHead.isVisible()) {
- int rowHeadWidth = this.rowHead.getWidth();
- borderR.x += rowHeadWidth;
- borderR.width -= rowHeadWidth;
- }
-
- if (this.vsb != null && this.vsb.isVisible()) {
- borderR.width -= this.vsb.getWidth();
- }
-
- if (this.hsb != null && this.hsb.isVisible()) {
- borderR.height -= this.hsb.getHeight();
- }
-
- return borderR;
- }
-
- public void layoutContainer(Container parent) {
- Rectangle availR = new Rectangle(((Component)parent).getSize());
- Insets insets = parent.getInsets();
- availR.x = insets.left;
- availR.y = insets.top;
- availR.width -= insets.left + insets.right;
- availR.height -= insets.top + insets.bottom;
- Rectangle colHeadR = new Rectangle(0, availR.y, 0, 0);
- if (this.colHead != null && this.colHead.isVisible()) {
- int colHeadHeight = this.colHead.getPreferredSize().height;
- colHeadR.height = colHeadHeight;
- availR.y += colHeadHeight;
- availR.height -= colHeadHeight;
- }
-
- Rectangle rowHeadR = new Rectangle(availR.x, 0, 0, 0);
- if (this.rowHead != null && this.rowHead.isVisible()) {
- int rowHeadWidth = this.rowHead.getPreferredSize().width;
- rowHeadR.width = rowHeadWidth;
- availR.x += rowHeadWidth;
- availR.width -= rowHeadWidth;
- }
-
- Border viewportBorder = ((JScrollPane)parent).getViewportBorder();
- Insets vpbInsets;
- if (viewportBorder != null) {
- vpbInsets = viewportBorder.getBorderInsets(parent);
- availR.x += vpbInsets.left;
- availR.y += vpbInsets.top;
- availR.width -= vpbInsets.left + vpbInsets.right;
- availR.height -= vpbInsets.top + vpbInsets.bottom;
- } else {
- vpbInsets = new Insets(0, 0, 0, 0);
- }
-
- colHeadR.x = availR.x;
- rowHeadR.y = availR.y;
- Component view = this.viewport != null ? this.viewport.getView() : null;
- Dimension viewSize = view != null ? view.getPreferredSize() : new Dimension(0, 0);
- Dimension extentSize = this.viewport != null ? this.viewport.toViewCoordinates(availR.getSize()) : new Dimension(0, 0);
- Rectangle vsbR = new Rectangle(0, availR.y - vpbInsets.top, 0, 0);
- boolean vsbNeeded = this.vsbPolicy == 22 || viewSize.height > extentSize.height && this.vsbPolicy == 20;
- if (this.vsb != null && vsbNeeded) {
- int vsbWidth = this.vsb.getPreferredSize().width;
- availR.width -= vsbWidth;
- vsbR.x = availR.x + availR.width + vpbInsets.right;
- vsbR.width = vsbWidth;
- }
-
- Rectangle hsbR = new Rectangle(availR.x - vpbInsets.left, 0, 0, 0);
- boolean hsbNeeded = this.hsbPolicy == 32 || viewSize.width > extentSize.width && this.hsbPolicy == 30;
- if (this.hsb != null && hsbNeeded) {
- int hsbHeight = this.hsb.getPreferredSize().height;
- availR.height -= hsbHeight;
- hsbR.y = availR.y + availR.height + vpbInsets.bottom;
- hsbR.height = hsbHeight;
- if (this.vsb != null && !vsbNeeded && this.vsbPolicy != 21) {
- extentSize = this.viewport.toViewCoordinates(availR.getSize());
- vsbNeeded = viewSize.height > extentSize.height;
- if (vsbNeeded) {
- int vsbWidth = this.vsb.getPreferredSize().width;
- availR.width -= vsbWidth;
- vsbR.x = availR.x + availR.width + vpbInsets.right;
- vsbR.width = vsbWidth;
- }
- }
- }
-
- vsbR.height = availR.height + vpbInsets.top + vpbInsets.bottom;
- hsbR.width = availR.width + vpbInsets.left + vpbInsets.right;
- rowHeadR.height = availR.height;
- colHeadR.width = availR.width;
- if (this.viewport != null) {
- this.viewport.setBounds(availR);
- }
-
- if (this.rowHead != null) {
- this.rowHead.setBounds(rowHeadR);
- }
-
- if (this.colHead != null) {
- this.colHead.setBounds(colHeadR);
- }
-
- if (this.vsb != null) {
- if (vsbNeeded) {
- this.vsb.setVisible(true);
- this.vsb.setBounds(vsbR);
- } else {
- this.vsb.setVisible(false);
- }
- }
-
- if (this.hsb != null) {
- if (hsbNeeded) {
- this.hsb.setVisible(true);
- this.hsb.setBounds(hsbR);
- } else {
- this.hsb.setVisible(false);
- }
- }
-
- if (this.lowerLeft != null) {
- this.lowerLeft.setBounds(rowHeadR.x, hsbR.y, rowHeadR.width, hsbR.height);
- }
-
- if (this.lowerRight != null) {
- this.lowerRight.setBounds(vsbR.x, hsbR.y, vsbR.width, hsbR.height);
- }
-
- if (this.upperLeft != null) {
- this.upperLeft.setBounds(rowHeadR.x, colHeadR.y, rowHeadR.width, colHeadR.height);
- }
-
- if (this.upperRight != null) {
- this.upperRight.setBounds(vsbR.x, colHeadR.y, vsbR.width, colHeadR.height);
- }
-
- }
-
- public Dimension minimumLayoutSize(Container parent) {
- Insets insets = parent.getInsets();
- int minWidth = insets.left + insets.right;
- int minHeight = insets.top + insets.bottom;
- if (this.viewport != null) {
- Dimension size = this.viewport.getMinimumSize();
- minWidth += size.width;
- minHeight += size.height;
- }
-
- Border viewportBorder = ((JScrollPane)parent).getViewportBorder();
- if (viewportBorder != null) {
- Insets vpbInsets = viewportBorder.getBorderInsets(parent);
- minWidth += vpbInsets.left + vpbInsets.right;
- minHeight += vpbInsets.top + vpbInsets.bottom;
- }
-
- if (this.rowHead != null && this.rowHead.isVisible()) {
- Dimension size = this.rowHead.getMinimumSize();
- minWidth += size.width;
- minHeight = Math.max(minHeight, size.height);
- }
-
- if (this.colHead != null && this.colHead.isVisible()) {
- Dimension size = this.colHead.getMinimumSize();
- minWidth = Math.max(minWidth, size.width);
- minHeight += size.height;
- }
-
- if (this.vsb != null && this.vsbPolicy != 21) {
- Dimension size = this.vsb.getMinimumSize();
- minWidth += size.width;
- minHeight = Math.max(minHeight, size.height);
- }
-
- if (this.hsb != null && this.hsbPolicy != 21) {
- Dimension size = this.hsb.getMinimumSize();
- minWidth = Math.max(minWidth, size.width);
- minHeight += size.height;
- }
-
- return new Dimension(minWidth, minHeight);
- }
-
- public Dimension preferredLayoutSize(Container parent) {
- Insets insets = parent.getInsets();
- int prefWidth = insets.left + insets.right;
- int prefHeight = insets.top + insets.bottom;
- Dimension extentSize = null;
- Dimension viewSize = null;
- Component view = null;
- if (this.viewport != null) {
- extentSize = this.viewport.getPreferredSize();
- viewSize = this.viewport.getViewSize();
- view = this.viewport.getView();
- }
-
- if (extentSize != null) {
- prefWidth += extentSize.width;
- prefHeight += extentSize.height;
- }
-
- Border viewportBorder = ((JScrollPane)parent).getViewportBorder();
- if (viewportBorder != null) {
- Insets vpbInsets = viewportBorder.getBorderInsets(parent);
- prefWidth += vpbInsets.left + vpbInsets.right;
- prefHeight += vpbInsets.top + vpbInsets.bottom;
- }
-
- if (this.rowHead != null && this.rowHead.isVisible()) {
- prefWidth += this.rowHead.getPreferredSize().width;
- }
-
- if (this.colHead != null && this.colHead.isVisible()) {
- prefHeight += this.colHead.getPreferredSize().height;
- }
-
- if (this.vsb != null && this.vsbPolicy != 21) {
- if (this.vsbPolicy == 22) {
- prefWidth += this.vsb.getPreferredSize().width;
- } else if (viewSize != null && extentSize != null) {
- boolean canScroll = true;
- if (view instanceof Scrollable) {
- canScroll = !((Scrollable)view).getScrollableTracksViewportHeight();
- }
-
- if (canScroll && viewSize.height > extentSize.height) {
- prefWidth += this.vsb.getPreferredSize().width;
- }
- }
- }
-
- if (this.hsb != null && this.hsbPolicy != 31) {
- if (this.hsbPolicy == 32) {
- prefHeight += this.hsb.getPreferredSize().height;
- } else if (viewSize != null && extentSize != null) {
- boolean canScroll = true;
- if (view instanceof Scrollable) {
- canScroll = !((Scrollable)view).getScrollableTracksViewportWidth();
- }
-
- if (canScroll && viewSize.width > extentSize.width) {
- prefHeight += this.hsb.getPreferredSize().height;
- }
- }
- }
-
- return new Dimension(prefWidth, prefHeight);
- }
-
- public void removeLayoutComponent(Component c) {
- if (c == this.viewport) {
- this.viewport = null;
- } else if (c == this.vsb) {
- this.vsb = null;
- } else if (c == this.hsb) {
- this.hsb = null;
- } else if (c == this.rowHead) {
- this.rowHead = null;
- } else if (c == this.colHead) {
- this.colHead = null;
- } else if (c == this.lowerLeft) {
- this.lowerLeft = null;
- } else if (c == this.lowerRight) {
- this.lowerLeft = null;
- } else if (c == this.upperLeft) {
- this.upperLeft = null;
- } else if (c == this.upperRight) {
- this.upperRight = null;
- }
-
- }
-
- public void setHorizontalScrollBarPolicy(int x) {
- switch (x) {
- case 30:
- case 31:
- case 32:
- this.hsbPolicy = x;
- return;
- default:
- throw new IllegalArgumentException("invalid horizontalScrollBarPolicy");
- }
- }
-
- public void setVerticalScrollBarPolicy(int x) {
- switch (x) {
- case 20:
- case 21:
- case 22:
- this.vsbPolicy = x;
- return;
- default:
- throw new IllegalArgumentException("invalid verticalScrollBarPolicy");
- }
- }
- }
-